
#include "Record.h"
#include <iostream.h>

Att :: Att () {
	data = 0;
	len = 0;
}

Att :: ~Att () {
	delete data;
	data = 0;
	len = 0;
}

int Att :: LoadInt (int loadMe) {

	int oldLen = len;
	delete data;
	len = sizeof (int);
	data = new char[len];
        memmove (data, &loadMe, len);
	return len - oldLen;
}

int Att :: LoadDouble (double loadMe) {

	int oldLen = len;
        delete data;
        len = sizeof (double);
        data = new char[len];
        memmove (data, &loadMe, len);
	return len - oldLen;
}

int Att :: LoadString (char *loadMe) {

	int oldLen = len;
        delete data;
        len = strlen (loadMe) + 1;
        data = new char[len];
        strcpy (data, loadMe);
	return len - oldLen;
}

int Att :: GetInt () {
	return *((int *) data);
}

double Att :: GetDouble () {
	return *((double *) data);
}

char *Att :: GetString () {
	return strdup (data);
}

int Att :: ToBinary (char *here) {

	// first, write out the size
	memmove (here, &len, sizeof(int));

	// then write out the data
	memmove (here + sizeof (int), data, len);

	// and return the number of bytes copied
	return len + sizeof (int);
}

int Att :: FromBinary (char *here) {

	// delete anything we now have
	delete data;
	
	// read in the size
	memmove (&len, here, sizeof(int));

	// allocate the new memory and read the bytes
	data = new char[len];
	memmove (data, here + sizeof (int), len);
	//cout << "Att with " << len + sizeof (int) << " bytes.\n";

	// return the number of bytes copied
	return len + sizeof (int);
}

// easy!  Just return the total size we'd be after a call to ToBinary
int Att :: GetLength () {
	return len + sizeof (int);
}


